package com.practice.book.service;

import com.practice.book.enums.BookStatusEnum;
import com.practice.book.mapper.BookMapper;
import com.practice.book.model.BookInfo;
import com.practice.book.model.PageRequest;
import com.practice.book.model.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-11
 * Time: 17:36
 */
@Slf4j
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {
        Integer len = pageRequest.getPageSize();
        System.out.println("pageSize的长度: "+len);
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest);
        for(BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResponse<>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public void batchDelete(List<Integer> ids) {
        bookMapper.batchDelete(ids);
    }
}
